package com.hatic.purchase;

import java.util.List;
import java.util.Map;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.DateUtil;
import org.jxstar.util.key.KeyCreator;

/**
 * 竞争性谈判采购事件处理
 * @author caojiaxin
 * @version 1.0,2011-10-28
 */
public class PurSayBO extends BusinessObject {

	private static final long serialVersionUID = 1L;

	/**
	 * 把采购单中供应商明细的记录导入到谈判记录报价明细中
	 * @param recordId
	 * @param userid
	 * @return
	 */
	public String onImproviders(String recordId,String userid){
		if(recordId == null || recordId.equals("")){
			_log.showDebug("取不到谈判记录ID！");
			return _returnFaild;
		}
		if(!insertProviders(recordId,userid)){
			setMessage("导入供应商明细出现错误！");
			return _returnFaild;
		}
		
		return _returnSuccess;
	}
	/**
	 * 1\提交检查供应商是否有3家以上
	 * 2\判断必须有一个中标单位才能提交
	 * @param recordId
	 * @return
	 */
	public String onAuditCheck(String[] recordId){
		
		if(recordId == null || recordId.length == 0){
			setMessage("请选择一条谈判记录！");
			return _returnFaild;
		}
		for (int i = 0; i < recordId.length; i++) {
			String msg=queryprovider(recordId[i]);
			if(!msg.equals("")){
				setMessage(msg);
				return _returnFaild;
			}
		}
		return _returnSuccess;
	}
	/**
	 * 提交时反馈中标单位的标志（是否选中）到采购单中
	 * @return
	 */
	public String onAuditUpdate(String[] recordId){
		if(recordId == null || recordId.length == 0){
			setMessage("请选择一条谈判记录！");
			return _returnFaild;
		}
		for (int i = 0; i < recordId.length; i++) {
			if(!updateProvider(recordId[i])){
				setMessage("提交时反馈中标单位出现错误！");
				return _returnFaild;
			}
		}
		
		return _returnSuccess;
	}

	/**
	 * 反馈中标单位的标志到采购单中
	 * @param recordId
	 * @return
	 */
	private boolean updateProvider(String recordId) {
		String selsql="select p.price_id,p.record_id,r.apply_id,p.provider_id from pur_say_record r inner join "+
		" pur_say_price p  on r.record_id=p.record_id"+ 
		" where p.record_id=? and p.say_result='1'";
		DaoParam param=_dao.createParam(selsql);
		param.addStringValue(recordId);
		List<Map<String, String>> mapList=_dao.query(param);
		_log.showDebug("===========execute sql={"+selsql+"}");
		
		String updatesql="update pur_provider set isbid = '1' where apply_id=? and provider_id=?";
		_log.showDebug("================execute sql={"+updatesql+"}");
		for(int i=0;i<mapList.size();i++){
			Map<String,String> map=mapList.get(i);
			DaoParam uparam=_dao.createParam(updatesql);
			uparam.addStringValue(map.get("apply_id"));
			uparam.addStringValue(map.get("provider_id"));
			if(!_dao.update(uparam)) return false;
		}
		return true;
	}
	
	/**
	 * 向报价明细中导入供应商
	 * @param recordId
	 * @param userid
	 * @return
	 */
	private boolean insertProviders(String recordId,String userid) {
		String sql="select apply_id,provider_id,provider_name from pur_provider "+
		"where apply_id = (select apply_id from pur_say_record where record_id=?) ";
		DaoParam qparam=_dao.createParam(sql);
		qparam.addStringValue(recordId);
		List<Map<String, String>> mapList=_dao.query(qparam);
		_log.showDebug("===========record_id="+recordId+";execute sql = "+sql);
		
		String sql2="insert into pur_say_price(price_id,record_id,provider_id,provider_name,say_result,add_userid,add_date) "+
		"values(?,?,?,?,?,?,?)";
		
		for(int i=0;i<mapList.size();i++){
			Map<String, String> map=mapList.get(i);
			String provider_id=map.get("provider_id");
			String provider_name=map.get("provider_name");
		
			String priceId=KeyCreator.getInstance().createKey("pur_say_price");
			
			DaoParam iparam=_dao.createParam(sql2);
			iparam.addStringValue(priceId);
			iparam.addStringValue(recordId);
			iparam.addStringValue(provider_id);
			iparam.addStringValue(provider_name);
			iparam.addStringValue("0");
			iparam.addStringValue(userid);
			iparam.addDateValue(DateUtil.getTodaySec());
			
			_log.showDebug("===========execute sql = "+sql2);
			boolean res=_dao.update(iparam);
			if(!res) return false;
		}
		
		return true;
	}
	
	/**
	 * 根据谈判记录ID查询报价明细中供应商数量是否少于3
	 * @param recordId
	 * @return
	 */
	private String queryprovider(String recordId) {
		String sql = "select count(*) as num from pur_say_price where record_id = ?";
		String sql2="  and say_result = '1' ";
		String msg="";
		
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(recordId);
		Map<String, String> map = _dao.queryMap(param);
		_log.showDebug("----------record_id=" + recordId + "; sql=" + sql);
		if(map == null) return "";
		int count = Integer.parseInt(map.get("num")) ;
		
		if(count<3){
			msg="竞争性谈判记录提交失败：要求报价明细中供应商至少3家！";
			return msg;
		}
		param=_dao.createParam(sql+sql2);
		param.addStringValue(recordId);
		Map<String, String> map2 = _dao.queryMap(param);
		_log.showDebug("----------record_id=" + recordId + "; sql=" + sql+sql2);
		if(map2 == null) return "";
		int count2 = Integer.parseInt(map2.get("num")) ;
		if(count2<1){
			msg="竞争性谈判记录提交失败：报价明细中供应商至少有一家中标！";
			
		}
		
		return msg;
	}
}
